package com.ATM;

import com.ATM.constants.Sqls;
import com.ATM.tools.DataUtils;

import javax.swing.*;
import java.awt.Font;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.time.LocalDateTime;

public class TransferPanel extends JPanel implements Sqls {
    private JTextField jt_balance;
    private JTextField jt_card;
    private JPasswordField jt_password;

    private int to_user_id;

    private String to_bank_card;

    private int to_user_balance;

    /**
     * Create the panel.
     */
    public TransferPanel(String loginCard) {
        setLayout(null);

        JLabel lblNewLabel = new JLabel("\u8F6C\u8D26");
        lblNewLabel.setFont(new Font("宋体", Font.BOLD, 20));
        lblNewLabel.setBounds(206, 56, 190, 57);
        add(lblNewLabel);

        JLabel jl_num = new JLabel("\u8F6C\u8D26\u91D1\u989D\uFF1A");
        jl_num.setFont(new Font("宋体", Font.BOLD, 15));
        jl_num.setBounds(106, 123, 107, 31);
        add(jl_num);

        JLabel lblNewLabel_2 = new JLabel("\u63A5\u6536\u8D26\u6237\uFF1A");
        lblNewLabel_2.setFont(new Font("宋体", Font.BOLD, 15));
        lblNewLabel_2.setBounds(106, 180, 86, 18);
        add(lblNewLabel_2);

        JLabel lblNewLabel_3 = new JLabel("\u5BC6\u7801\u786E\u8BA4\uFF1A");
        lblNewLabel_3.setFont(new Font("宋体", Font.BOLD, 15));
        lblNewLabel_3.setBounds(106, 224, 86, 31);
        add(lblNewLabel_3);

        jt_balance = new JTextField();
        jt_balance.setBounds(189, 123, 107, 26);
        add(jt_balance);
        jt_balance.setColumns(10);

        jt_card = new JTextField();
        jt_card.setBounds(189, 174, 107, 26);
        add(jt_card);
        jt_card.setColumns(10);

        jt_password = new JPasswordField();
        jt_password.setBounds(189, 224, 107, 26);
        add(jt_password);
        jt_password.setColumns(10);

        JButton jb_find = new JButton("\u67E5\u8BE2\u8D26\u6237");
        jb_find.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                String cardText = jt_card.getText();
//				获取余额
                ResultSet set = null;
                try {
                    set = DataUtils.query(sql_findByCard, new Object[]{cardText});
                    if (set.next()) {
                        to_user_id = set.getInt("user_id");
                        to_bank_card = set.getString("bank_card");
                        String name = set.getString("name");
                        to_user_balance = set.getInt("balance");
                        JOptionPane.showMessageDialog(null, "请确认转账账户: 卡号：" +
                                to_bank_card + ",姓名：" + name);
                    } else {
                        JOptionPane.showMessageDialog(null, "该账户不存在！");
                    }
                } catch (SQLException ex) {
                    throw new RuntimeException(ex);
                }
            }
        });
        jb_find.setFont(new Font("宋体", Font.BOLD, 16));
        jb_find.setBounds(317, 174, 107, 24);
        add(jb_find);

        JButton jb_ok = new JButton("\u786E\u8BA4\u8F6C\u8D26");
        jb_ok.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                String balanceText = jt_balance.getText();
                if (balanceText.equals("")) {
                    JOptionPane.showMessageDialog(null, "请输入转账金额！");
                } else {
                    //				转账金额
                    int transferBalance = Integer.parseInt(balanceText);
//				用户输入的密码
                    String passwordText = String.valueOf(jt_password.getPassword());
                    String loginPassword = null;
//				登陆者id
                    int loginId = 0;
                    int loginBalance = 0;
                    try {
                        ResultSet set = DataUtils.query(sql_findByCard, new Object[]{loginCard});
                        if (set.next()) {
                            loginId = set.getInt("user_id");
                            loginBalance = set.getInt("balance");
                            loginPassword = set.getString("password");
                        }
                    } catch (SQLException ex) {
                        throw new RuntimeException(ex);
                    }
//				转账人密码校验
                    if (!passwordText.equals(loginPassword)) {
                        JOptionPane.showMessageDialog(null, "密码不正确！");
                    } else {
                        int a, b, c;
//				更新转账人账户

                        int balance = loginBalance - transferBalance;
//                        流水
                        String sql = "insert into running values(0,?,'转账',?,?,?)";
                        try {
                            DataUtils.Update(sql, new Object[]{loginCard, transferBalance, balance, LocalDateTime.now()});
                        } catch (SQLException ex) {
                            throw new RuntimeException(ex);
                        }
                        try {
                            a = DataUtils.Update(sql_save_balance, new Object[]{balance, loginCard});
                        } catch (SQLException ex) {
                            throw new RuntimeException(ex);
                        }
//				更新目标转账人账户

                        int balance = loginBalance - transferBalance;
//                        流水
                        String sql = "insert into running values(0,?,'转账',?,?,?)";
                        try {
                            DataUtils.Update(sql, new Object[]{loginCard, transferBalance, balance, LocalDateTime.now()});
                        } catch (SQLException ex) {
                            throw new RuntimeException(ex);
                        }
                        try {
                            a = DataUtils.Update(sql_save_balance, new Object[]{balance, loginCard});
                        } catch (SQLException ex) {
                            throw new RuntimeException(ex);
                        }
//				更新目标转账人账户
                        int user_balance = to_user_balance + transferBalance;
                        String sql2 = "insert into running values(0,?,'转账',?,?,?)";
                        try {
                            DataUtils.Update(sql2, new Object[]{
                                    to_bank_card, transferBalance, user_balance, LocalDateTime.now()
                            });
                        } catch (SQLException ex) {
                            throw new RuntimeException(ex);
                        }
                        try {
                            b = DataUtils.Update(sql_save_balance, new Object[]{user_balance, to_bank_card});
                        } catch (SQLException ex) {
                            throw new RuntimeException(ex);
                        }
//				添加转账记录
                        try {
                            c = DataUtils.Update(sql_transaction_log, new Object[]{
                                    loginId, to_user_id, transferBalance, LocalDateTime.now()
                            });
                        } catch (SQLException ex) {
                            throw new RuntimeException(ex);
                        }
                        if ((a + c + b) == 3) {
                            JOptionPane.showMessageDialog(null, "" +
                                    "向:卡号" + to_bank_card + "转账" + transferBalance + "元 成功！");
                        } else {
                            JOptionPane.showMessageDialog(null, "转账失败！");
                        }
                    }
                }
            }
        });
        jb_ok.setFont(new Font("宋体", Font.BOLD, 16));
        jb_ok.setBounds(162, 284, 134, 37);
        add(jb_ok);

    }

}
